LineOfPlay.prototype = new Array();

var Direction = {LEFT: 0, UP: 1, RIGHT: 2, DOWN: 3};

function LineOfPlay(topLeftBounds, bottomRightBounds) {
	Array.call(this);
	
	this.topDirection = Direction.LEFT;
	
	this.topPosition = new Vector2(0.5 * (bottomRightBounds.x + topLeftBounds.x),
											0.5 * (bottomRightBounds.y + topLeftBounds.y));
	
	this.topPips;
	
	this.bottomDirection = Direction.RIGHT;
	this.bottomPosition;
	this.bottomPips;
	
	this.topLeft = new Vector2(topLeftBounds);
	this.bottomRight = new Vector2(bottomRightBounds);		
}

LineOfPlay.prototype.isTop = function(piece) {
	
	if(this.length == 0) { return true; }
	
	return piece.topPips == this.topPips || piece.bottomPips == this.topPips;
}

LineOfPlay.prototype.unshift = function(piece) {		

	var pieceWidth = piece.sprite.width;
	var pieceHeight = piece.sprite.height;			
								
	if(this.topDirection == Direction.LEFT) {
					
		this.topPosition.translate(-pieceHeight, 0);
		
		piece.sprite.rotate(-Math.PI / 2);			
		
		if(piece.topPips == this.topPips) {				
			piece.invert();				
		}
		
		if(piece.isDouble() && this.topPosition.x > this.topLeft.x) {				
		
			piece.sprite.isRotated = false;			
			piece.sprite.translate(pieceWidth, -0.5 * pieceWidth);
							
			this.topPosition.translate(pieceWidth, 0);														
		}
		
		// First up play here because of double pieces 
		if(this.topPosition.x <= this.topLeft.x) {							
	 		this.topDirection = Direction.UP;
	 		
	 		this.topPosition.translate(pieceWidth, -pieceWidth);		 				 			
	 	}
	 	 						 		 	
	 	this.topPips = piece.topPips;								 		
	}	

	else if (this.topDirection == Direction.UP) {	 	
								
		this.topPosition.translate(0, -pieceHeight);
		
		if(piece.topPips == this.topPips) {
			
			piece.invert();
		}
		
		if(piece.isDouble()) {
		
			piece.sprite.rotate(Math.PI / 2);				
			
			piece.sprite.translate(-0.5 * pieceWidth, pieceWidth);
			
			var lastPiece = this.head();	
			if(lastPiece.sprite.isRotated && !lastPiece.isDouble()) {
					
				lastPiece.sprite.isRotated = false;
				lastPiece.sprite.translate(pieceWidth, -pieceWidth);
				
				this.topPosition.translate(pieceWidth, 0);																							
			}								
		}					
		
		this.topPips = piece.topPips;
	
		if(this.topPosition.y < this.topLeft.y) {		
			//window.alert("LEFT!");					
			this.topDirection = Direction.RIGHT;
			this.topPosition.translate(pieceWidth, pieceHeight);
		}									
	}
/*
	else if(this.topDirection == Direction.RIGHT) {
		
		this.topPosition.translate(pieceHeight, 0);
		
		piece.sprite.rotate(Math.PI / 2);
		
		if(piece.topPips == this.topPips) {				
			piece.invert();				
		}
	
		if(piece.isDouble() && this.topPosition.x + pieceHeight < this.bottomRight.x) {				
		
			piece.sprite.isRotated = false;			
			piece.sprite.translate(0, -0.5 * pieceWidth);
			
			topPosition.translate(-pieceWidth, 0);																						
		}
	
		this.topPips = piece.topPips;	
	
		if(this.topPosition.x + pieceHeight >= this.bottomRight.x) {
			window.alert("goin'down: " + piece);	
			this.topDirection = Direction.DOWN;
			this.topPosition.translate(-pieceWidth, pieceWidth);																		
		}							
	}
	
	else if(this.topDirection == Direction.DOWN) {
	
		piece.sprite.move(this.topPosition);	
		this.topPosition.translate(0, pieceHeight);
				
		if(piece.bottomPips == this.topPips) {				
			piece.invert();				
		}									
			
		this.topPips = piece.bottomPips;
			
		if(this.topPosition.y > this.bottomRight.y) {
			window.alert("blim");
			this.topDirection = Direction.LEFT;			
		}
	}												

		*/
	
	piece.sprite.move(this.topPosition);
	
	Array.prototype.unshift.call(this, piece);		
		
	piece.sprite.setTurnedUp(true);
	piece.sprite.clickCallback = null;
	piece.sprite.hoverCallback = null;	
	
}
/*
function playPieceOld(hand, i) {			
		
	var playedPiece = hand[i];
	var hasPlayed = false;
	
	var pieceWidth = playedPiece.sprite.width;
	var pieceHeight = playedPiece.sprite.height;
	
	var computerPosition = computerHand.tail().sprite.position;
	var humanPosition = humanHand.tail().sprite.position;					
	
	// First move
	if(lineOfPlay.length == 0) {		
		
		playedPiece.sprite.move(0.5 * canvas.width - 0.5 * 5 * pieceWidth,
										0.5 * canvas.height - 0.5 * pieceHeight);
				
		topPosition = new Vector2(playedPiece.sprite.position);							
		
		if(playedPiece.isDouble()) {																									
			topPosition.translate(-pieceHeight, 1/3 * pieceWidth);			
		}
		else {
			topPosition.translate(-pieceHeight, 0);		
			playedPiece.sprite.rotate(-Math.PI / 2);											
		}
		
		bottomPosition = new Vector2(playedPiece.sprite.position.x + pieceWidth,
											playedPiece.sprite.position.y + 1/3 * pieceWidth);
											
		topPips = playedPiece.topPips;
		bottomPips = playedPiece.bottomPips;																						
		
		lineOfPlay.push(playedPiece);		
		hasPlayed = true;						
	}	
	
	// Top
	else if(playedPiece.topPips == topPips || 
				playedPiece.bottomPips == topPips) {
			
		playedPiece.sprite.move(topPosition);					
		
		if(topDirection == Direction.LEFT) {
							
			topPosition.translate(-pieceHeight, 0);
			
			playedPiece.sprite.rotate(-Math.PI / 2);			
			
			if(playedPiece.topPips == topPips) {
				playedPiece.invert();
			}
			
			if(playedPiece.isDouble() && topPosition.x > 0) {
				
				playedPiece.sprite.isRotated = false;
				playedPiece.sprite.translate(pieceWidth, -0.5 * pieceWidth);
				
				topPosition.translate(pieceWidth, 0);
			}
					
			if(topPosition.x < 0) {
		 		topDirection = Direction.UP;
		 		topPosition.translate(pieceHeight, -pieceHeight);
		 	}
		 	
		 	topPips = playedPiece.topPips;
		}
		
		else if (topDirection == Direction.UP) {
				
			topPosition.translate(0, -pieceHeight);
			
			if(playedPiece.topPips == topPips) {
				
				playedPiece.invert();
			}
		
			if(playedPiece.isDouble() && topPosition.y > 0) {
				
				playedPiece.sprite.setRotated(true);
				//playedPiece.sprite.translate(pieceWidth, -0.5 * pieceWidth);
				
				//topPosition.translate(pieceWidth, 0);
			}
			
			if(topPosition.y < this.topLeft.y) {
				topDirection = Direction.RIGHT;
				topPosition.translate(0, pieceWidth);
			}
			
			topPips = playedPiece.topPips;
		}
								
		else if(topDirection == Direction.RIGHT) {
			
			topPosition.translate(pieceHeight, 0);
			
			playedPiece.sprite.rotate(Math.PI / 2);
			
			if(playedPiece.topPips == topPips) {
				
				playedPiece.invert();				
			}
		
			if(topPosition.x > canvas.width - 7 * pieceWidth) {
				topDirection = Direction.DOWN;															
			}
		
			topPips = playedPiece.topPips;			
		}
	
		else if(topDirection == Direction.DOWN) {
	
			playedPiece.sprite.move(topPosition);	
			topPosition.translate(0, pieceHeight);
				
			if(playedPiece.bottomPips == topPips) {				
				playedPiece.invert();				
			}
			
			lineOfPlay.unshift(playedPiece);
			hasPlayed = true;
			
			topPips = playedPiece.bottomPips;
			
			if(topPosition.y > handPosition.y + 3 * pieceHeight) {
				window.alert("blim");
				lineDirection = Direction.LEFT;			
			}
		}										
		
		lineOfPlay.unshift(playedPiece);
		hasPlayed = true;																		
	}	

	// Bottom
	else if(playedPiece.topPips == lineOfPlay.tail().bottomPips || 		 
				playedPiece.bottomPips == lineOfPlay.tail().bottomPips) {													
				
		playedPiece.sprite.move(bottomPosition);
		
		if(bottomDirection == Direction.RIGHT) {
				
			playedPiece.sprite.rotate(-Math.PI / 2);
			
			bottomPosition.translate(pieceHeight, 0);																					
		
			if(playedPiece.bottomPips == lineOfPlay.tail().bottomPips) {
			
				playedPiece.invert();	
			}				
							
			if(bottomPosition.x + pieceHeight > canvas.width - 4 * pieceWidth)	{
				
				bottomDirection = Direction.DOWN;
				bottomPosition.translate(-pieceWidth, pieceWidth);			
			}										
		}
		
		else if(bottomDirection == Direction.DOWN) {						
		 	
		 	bottomPosition.translate(0, pieceHeight);
		 			 
		 	if(playedPiece.bottomPips === lineOfPlay.tail().bottomPips) {				
					playedPiece.invert();	
			}
		 	
		 	if(bottomPosition.y >= humanPosition.y - 3 * pieceWidth) {														
				
				bottomDirection = Direction.LEFT;
				bottomPosition.translate(-pieceWidth, 0);
		 	}
		 	
		}
		
		else if(bottomDirection == Direction.LEFT) {
			
			bottomPosition.translate(-pieceHeight, 0);
			playedPiece.sprite.rotate(+Math.PI / 2);
			
			if(playedPiece.bottomPips === lineOfPlay.tail().bottomPips) {				
				
				playedPiece.invert();	
			}		
			
			if(bottomPosition.x <= pieceWidth) {
				bottomDirection = Direction.UP;
				bottomPosition.translate(pieceWidth, -pieceWidth);			
			}
		}		
		
		else if(bottomDirection == Direction.UP) {
			bottomPosition.translate(0, -pieceHeight);
		}
				
		hasPlayed = true;			
		lineOfPlay.push(playedPiece);									
	}
	
	if(hasPlayed) {
		hand.remove(playedPiece);		
	}

	return hasPlayed;
}
*/